import { defineStore } from 'pinia'
import $ajax from '@/api/to8050.js'
import router from '@/router/index.js'
import { showErrorMsg } from '@/utils/alterMessage.js'

export const useMenuStore = defineStore('menu', {
  state: () => ({
    isCollapse: false,
    menuList: []
  }),
  getters: {},
  actions: {
    async getMenu() {
      if (this.menuList === null || this.menuList.length === 0) {
        const roleId = sessionStorage.getItem('roleId')
        // console.log(roleId)
        // console.log(JSON.parse(sessionStorage.getItem('currentUser')))
        if (roleId === undefined || roleId === null) {
          await router.push({ path: '/login' })
          return
        }
        const path = '/sysNode/select/menu/' + roleId
        const res = await $ajax.get(path)
        if (res !== null && res.code != null && res.code === 200) {
          this.menuList = res.result
        } else {
          showErrorMsg('获取菜单失败')
          await router.push({ path: '/login' })
        }
      }
    }
  }
})
